﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UnityCore;
using DataManager;
using StoreClientModule;

namespace MBBStoreClient
{
    public partial class frmProfileViewer : Form
    {
        ProfileManager profManager = new ProfileManager();
        int pageNum = 1;
        int recLimit = 50;
        SearchInfo keyword = new SearchInfo();
        ProfileInfo profile = new ProfileInfo();

        public frmProfileViewer()
        {

            InitializeComponent();

            btnClose.Click += new EventHandler(btnClose_Click);
            panel2.EnabledChanged += new EventHandler(panel2_EnabledChanged);
            
            profManager.ProcessCompleted += new CoreHandler(profManager_ProcessCompleted);
            profManager.ProcessError += new CoreHandler(profManager_ProcessError);
            txtKeyword.KeyPress += new KeyPressEventHandler(txtKeyword_KeyPress);
            btnDoSearch.Click += new EventHandler(btnDoSearch_Click);
            btnNext.Click += new EventHandler(btnNext_Click);
            btnPrev.Click += new EventHandler(btnPrev_Click);
            grdView.DoubleClick += new EventHandler(grdView_DoubleClick);
            btnRefresh.Click += new EventHandler(btnRefresh_Click);
        }

        void btnRefresh_Click(object sender, EventArgs e)
        {
            btnDoSearch_Click(null, null); 
        }

        

        void grdView_DoubleClick(object sender, EventArgs e)
        {
            if (grdView.SelectedRows.Count == 1)
            {
                string refNum = grdView.SelectedRows[0].Cells[0].Value.ToString();
                string tmpSReference = "";
                string tmpCReference = "";
                int cnt = 0;

                if (refNum.StartsWith("A"))
                {
                    for (int i = 0; i < refNum.Length; i++)
                    {
                        if (refNum[i].ToString().Equals("A"))
                        {
                            cnt++;
                            tmpCReference += "B";
                        }
                    }
                    profile.ChargeReferenceNumber = tmpCReference + "" + refNum.Substring(cnt, refNum.Length-1);
                    profile.SalesReferenceNumber = refNum;
                }
                else
                {
                    for (int i = 0; i < refNum.Length; i++)
                    {
                        if (refNum[i].ToString().Equals("B"))
                        {
                            cnt++;
                            tmpSReference += "A";
                        }
                    }
                    profile.SalesReferenceNumber = tmpSReference + "" + refNum.Substring(cnt, refNum.Length-1);
                    profile.ChargeReferenceNumber = refNum;
                }

                profile.name = grdView.SelectedRows[0].Cells[1].Value.ToString();
                profile.address = grdView.SelectedRows[0].Cells[2].Value.ToString();
                profile.contactNumber = grdView.SelectedRows[0].Cells[3].Value.ToString();
                profile.date = DateTime.Parse(grdView.SelectedRows[0].Cells[4].Value.ToString());
                profile.status = grdView.SelectedRows[0].Cells[5].Value.ToString();
                
                Common.parent.EditProfile(profile);
            }
        }

        

        void btnPrev_Click(object sender, EventArgs e)
        {
            pageNum--;
            btnDoSearch_Click(null, null); 
        }

        void btnNext_Click(object sender, EventArgs e)
        {
            pageNum++;
            btnDoSearch_Click(null, null);
        }
        
        void btnDoSearch_Click(object sender, EventArgs e)
        {
            keyword.keyword = txtKeyword.Text;
            keyword.start = recLimit * (pageNum-1);
            profManager.ViewProfile(keyword);
        }

        void txtKeyword_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                keyword.keyword = txtKeyword.Text;
                keyword.start = recLimit * (pageNum - 1);
                profManager.ViewProfile(keyword);
            }
        }
        void profManager_ProcessError(string pcode, string msg)
        {
            uCoreClasses.uThreader t = new uCoreClasses.uThreader();
            t.SafeCall(this, delegate
            {
                MessageBox.Show(msg);
            });
        }

        void profManager_ProcessCompleted(string pcode, string msg)
        {
            uCoreClasses.uThreader t = new uCoreClasses.uThreader();
            t.SafeCall(this, delegate
            {
                if (pcode == "ViewProfile")
                {
                    
                    grdView.DataSource = profManager.exportTable;
                    grdView.Columns[0].HeaderText = "Reference Number";
                    grdView.Columns[1].HeaderText = "Name";
                    grdView.Columns[2].HeaderText = "Address";
                    grdView.Columns[3].HeaderText = "Contact Number";

                    if (profManager.exportTable.Rows.Count == recLimit)
                    {
                        if (pageNum == 1)
                        {
                            btnNext.Visible = true;
                            btnPrev.Visible = false;
                        }
                        else
                        {
                            btnNext.Visible = true;
                            btnPrev.Visible = true;
                        }
                    }
                    else
                    {
                        if (pageNum != 1)
                        {
                            btnPrev.Visible = true;
                            btnNext.Visible = false;
                        }
                        else
                        {
                            btnNext.Visible = false;
                            btnPrev.Visible = false;
                        }
                    }
                    lblPage.Text = "- Page " + pageNum + " -";
                    if(profManager.exportTable.Rows.Count == 0)
                        lblPage.Text = "";
                }
                
            });
        }
        void panel2_EnabledChanged(object sender, EventArgs e)
        {
            if (!panel2.Enabled)
            {
                panel2.BackColor = Color.MidnightBlue;
            }
            else
            {
                panel2.BackColor = Color.RoyalBlue;
            }
        }
        void btnClose_Click(object sender, EventArgs e)
        {
            pnlForm.Dispose();
            this.Dispose();
        }
        public Panel PanelForm
        {
            get { return pnlForm; }
        }
        public void Initialize(string Title)
        {
            this.Select();
            lblText.Text = Title;
        }
    }
}
